package com.shishuo.cms.action.manage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.shishuo.cms.constant.AttachmentConstant;
import com.shishuo.cms.constant.UploadUtils;
import com.shishuo.cms.exception.UploadException;

@Controller
@RequestMapping("/admin/attachment")
public class AdminAttachmentAction extends AdminBaseAction{
	@ResponseBody
	@RequestMapping(value = "/ueditor/upload.htm", method = RequestMethod.POST)
	public String photoUpload(@RequestParam("kindId") long kindId,
			@RequestParam(value = "kind") AttachmentConstant.Kind kind,
			@RequestParam("Filename") String Filename,
			@RequestParam("fileName") String fileName,
			@RequestParam("fileNameFormat") String fileNameFormat,
			@RequestParam("pictitle") String pictitle,
			@RequestParam("Upload") String Upload,
			@RequestParam("upfile") MultipartFile upfile,
			HttpServletRequest request){
		JSONObject json = new JSONObject();
		if (!UploadUtils.isFileType(fileName, UploadUtils.PHOTO_TYPE)) {
			json.put("state", "不允许的文件格式");
			return json.toString();
		}
		try {
			Date now = new Date();
			String basePath = request.getSession().getServletContext().getRealPath("/");
			String uploadPath = UploadUtils.getUploadPath(basePath,fileName, now.getTime());
			upfile.transferTo(new java.io.File(basePath+uploadPath));
			json.put("original", fileName);
			json.put("url", uploadPath);
			json.put("title", pictitle);
			json.put("state", "SUCCESS");
		} catch (Exception e) {
			json.put("state", e.getMessage());
		}
		return json.toString();
	}
	@ResponseBody
	@RequestMapping(value = "/ueditor/manager.htm", method = RequestMethod.POST)
	public String photoManager(@RequestParam(value = "kindId") long kindId,
			@RequestParam(value = "kind") AttachmentConstant.Kind kind,
			HttpServletRequest request) {
		List<String> picturePathList = new ArrayList<String>();
		String basePath = request.getSession().getServletContext().getRealPath("/");
		picturePathList=UploadUtils.getFiles(basePath,basePath+"\\upload\\", picturePathList, UploadUtils.PHOTO_TYPE);
		return StringUtils.join(picturePathList.toArray(), "ue_separate_ue");
	}
	@ResponseBody
	@RequestMapping(value = "/upload.json", method = RequestMethod.POST)
	public String upload(@RequestParam(value = "fileType",defaultValue="file") String fileType,
			@RequestParam(value = "file") MultipartFile file,
			HttpServletRequest request) {
		String fileName=file.getOriginalFilename();
		JSONObject json = new JSONObject();
		if (!UploadUtils.isFileType(fileName, fileType=="file"?UploadUtils.FILE_TYPE:UploadUtils.PHOTO_TYPE)) {
			json.put("state", "不允许的文件格式");
			return json.toString();
		}try {
			Date now = new Date();
			String basePath = request.getSession().getServletContext().getRealPath("/");
			String uploadPath = UploadUtils.getUploadPath(basePath,fileName, now.getTime());
			file.transferTo(new java.io.File(basePath+uploadPath));
			json.put("original", fileName);
			json.put("url", uploadPath);
			json.put("state", "SUCCESS");
		} catch (Exception e) {
			json.put("state", e.getMessage());
		}
		return json.toString();
	}
}
